/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：User_addr_book接口实现                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Repository.SqlServer                                  
*│　类    名： User_addr_bookRepository                                      
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.DbHelper;
using NetCoreCMS.IRepository;
using NetCoreCMS.Models;
using Dapper;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using System.Transactions;
using System.Data;

namespace NetCoreCMS.Repository.SqlServer
{
    public class User_addr_bookRepository:BaseRepository<User_addr_book,Int32>, IUser_addr_bookRepository
    {
        public User_addr_bookRepository(IOptionsSnapshot<DbOption> options)
        {
            _dbOption =options.Get("NetCoreCMS");
            if (_dbOption == null)
            {
                throw new ArgumentNullException(nameof(DbOption));
            }
            //_dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString);
        }

        public void SetDefault(int id, string user_name)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                
                    _dbConnection.Execute("update [" + nameof(User_addr_book) + "] set is_default=0 where user_name=@user_name", new { user_name = user_name });
                    _dbConnection.Execute("update [" + nameof(User_addr_book) + "] set is_default=1 where id=@id and user_name=@user_name", new { id = id, user_name = user_name });
                    scope.Complete();
                }
            }
        }
    }
}